<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/8/2 0002
 * Time: 11:37
 */

namespace app\api\controller;

use app\service\MemberService;
use app\service\WxService;
use Redis\Redis;
use think\Db;
use think\facade\Request;

/**
 * 用户
 * @author   juzi
 * @blog    https://blog.csdn.net/juziaixiao
 * @version  0.0.1
 * @datetime 2020年7月22日10:57:35
 */
class User extends Common
{
    /**
     * [__construct 构造方法]
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月22日10:57:35
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();
        $this->wxservice = new WxService(['wechat' => true]);

    }

    /**
     * [Reg 用户注册-数据添加]
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月22日10:57:35
     */
    public function Reg()
    {

        // 调用服务层
        $params=$this->data_post;
        Db::table("comback")->insert(array("content"=>json_encode($params),'name'=>'注册经纬度'));
        $params['unionid'] =$params['unionId'];

        if (isset($params['ish5']))
        {
            $params['openid'] =$params['openId'];

//            $params['openId'] = $this->member['openid'];
//            $params['unionid'] = $this->member['unionid'];
            #在这根据经纬度 获取省市区


        }


        return MemberService::AppReg($params);
    }

    /**
     * [RegVerifySend 用户注册-验证码发送]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-05T19:17:10+0800
     */
    public function VerifySend()
    {

        // 调用服务层
        return MemberService::AppUserBindVerifySend($this->data_post);
    }


    /*
     * 用户登录
     */
    public function Login(){
        $params=$this->data_post;
        $re=MemberService::AppLogin($params);
        $redis = new Redis();
        $redis->hDel('SmsCode',$params['phone']);

        return  json(DataReturn('登录成功','0',['token'=>$re['data']]));
    }


    /**
     * [logbrowse 浏览记录]
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月22日10:57:35
     */
    public function LogBrowse()
    {

        $this->Islogin();
        $re = MemberService::LogBrowse(['member_id'=>$this->member_id]);

        return json(DataReturn('获取成功','0',$re['data']));
    }


    /**
     * [ResetInfo 账号设置]
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月22日10:57:35
     */
    public function ResetInfo()
    {

        $this->Islogin();
        $params=$this->data_post;
        $params['member_id']=$this->member_id;
        $params['token']=Request::header('token');

        // 调用服务层
        return MemberService::AppReg($params);
    }
    /**
     * [Logout 账号退出]
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月22日10:57:35
     */
    public function Logout()
    {
        MemberService::LogOut(['token'=>Request::header('token')]);
        return json(DataReturn('退出成功',0));
    }

    /**
     * [GetUserInfo 账号退出]
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月22日10:57:35
     */
    public function GetUserInfo()
    {
        $this->Islogin();

        return json(DataReturn('获取成功',0,$this->member));

    }
    /**
     * [GetMemberInfo app 请求用户信息]
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月22日10:57:35
     */
    public function GetMemberInfo()
    {
        $openid_app  = input("post.openId_app");
        $unionid = input("post.unionid");//用来修改h5的状态
        if(!empty($openid_app)){
            //根据App 的openid openId_app 查询 ish5 的状态 如果是1 改为2
            $AppMemberInfoHandle = new MemberService();
            #根据openId_app 获取用户信息
            $member = Db::table("member")
                ->field("id,unionid unionId,name nickName,headpic avatarUrl,openid_app openId,province,city,phone,ish5")
                ->where(array('unionid'=>$unionid))->find();

            if(!empty($member)){
                #修改openid_app
                if(empty($member['openId'])){
                    Db::table("member")->where(array('unionid'=>$unionid))->update(array('openid_app'=>$openid_app));
                }
                $member['openId'] = $openid_app;

                $member['token'] = $AppMemberInfoHandle->AppMemberInfoHandle($member['id']) ;
                //
                if($member['ish5'] == 1){
                    Db::table("member")
                        ->where(array('unionid'=>$unionid))
                        ->update(array('ish5'=>2));
                }

                return array("status"=>true,'minfo'=>true,'data'=>$member);
            }else{
                return array("status"=>true,'minfo'=>false);

            }

        }

    }
    /**
     * [UpdateInfo 修改用户信息]
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月22日10:57:35
     */
    public function UpdateInfo(){
        $param=$this->data_post;
//        $id=$this->member_id;
        $re=MemberService::UpdateInfo(1,$param);
        return  json(DataReturn('登录成功','0',['token'=>$re['data']]));
    }
    /**
     * [userish 查询修改用户ish5 状态]
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月22日10:57:35
     */
    function userish(){
        $unionid = input("post.unionid");
        $mark=input("post.mark");
        if(!empty($mark)){
            #设备id
            $cid = input("post.cid");
            #app 首页访问
            Db::table('member')->where(array("unionid"=>$unionid))->update(array("ish5"=>2));
            if(!empty($cid)){
                Db::table('member')->where(array("unionid"=>$unionid))->update(array("client_id"=>$cid));
            }
            return json(array("status"=>true));
        }else{
            if(!empty($unionid)){
                #h5访问 查询用户ish5状态
                $info =  Db::table('member')->field("ish5")->where(array("unionid"=>$unionid))->find();
            }else{
                #h5访问 查询用户ish5状态
                $info =  Db::table('member')->field("ish5")->where(array("unionid"=>input("post.uid")))->find();
            }
            if(!empty($info)){
                return json(array("status"=>true,'data'=>$info));

            }else{
                return json(array("status"=>true));

            }

        }


    }

}